/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import dao.AdministradorDao;
import dao.EnderecoDao;
import dao.ImovelDao;
import dao.ProprietarioDao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;
import model.Administrador;
import model.Endereco;
import model.Imovel;
import model.Proprietario;
import dao.implementacoes.AdministradorDaoImpl;

/**
 *
 * @author cristiane
 */
public class ImobiliariaServlet extends HttpServlet {



    protected void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        String cmd = request.getParameter("cmd");
        if(cmd == null)
            cmd = "inicial";
        ImovelDao imovelDao;
        AdministradorDao adminDao;
        ProprietarioDao propDao;
        Imovel imovel = new Imovel();
        Administrador admin = new Administrador();
        Proprietario prop = new Proprietario();
        Endereco end = new Endereco();
        

        if(cmd.equalsIgnoreCase("salvarAdmin") || cmd.equalsIgnoreCase("atualizarAdmin")
                || cmd.equalsIgnoreCase("atuAdmin") || cmd.equalsIgnoreCase("addAdmin") || cmd.equalsIgnoreCase("excAdmin")){
            admin.setNome(request.getParameter("nome"));
            admin.setCodigo(request.getParameter("codigo"));
            admin.setCpf(request.getParameter("cpf"));
            admin.setTelefone1(request.getParameter("telefone1"));
            admin.setTelefone2(request.getParameter("telefone2"));
            admin.setTelefone3(request.getParameter("telefone3"));
            admin.setTelefone4(request.getParameter("telefone4"));
            admin.setTipo(request.getParameter("tipo"));
            admin.setEmail(request.getParameter("email"));
            admin.setUsuario(request.getParameter("usuario"));
            admin.setSenha(request.getParameter("senha"));
        }

        try {
            adminDao = new AdministradorDaoImpl();
        } catch (Exception e) {
            //e.printStackTrace( );
            throw new ServletException(e);
        }

        RequestDispatcher rd = null;

        if(cmd.equalsIgnoreCase("autenticarUsuario")) {
            try {
                    HttpSession sessao = request.getSession(true);
                    admin = adminDao.procurarAdministrador(request.getParameter("usuario"), request.getParameter("senha"));
                    sessao.removeAttribute("admin");
                    sessao.setAttribute("logado", true);
                    sessao.setAttribute("usuario", admin.getUsuario());
                    sessao.setAttribute("tipoUsuario", admin.getTipo());
                    if (admin.getCpf() != null && !admin.getCpf().trim().equals("")) {
                        rd = request.getRequestDispatcher("paginaAdmin.jsp");
                    } else {
                        request.setAttribute("msgErro", "Erro na autenticacaoo. Usuario ou senha incorreto(s).");
                        rd = request.getRequestDispatcher("login.jsp");
                    }
            } catch (Exception e) {
                        request.setAttribute("msgErro", "Erro na autenticacao. Usuario ou senha incorreto(s).");
                        rd = request.getRequestDispatcher("login.jsp");
          //  } finally {
            //            rd.forward(request, response);
            }
        }else if(cmd.equalsIgnoreCase("logout")){
                HttpSession sessao = request.getSession(true);
                sessao.removeAttribute("usuario");
                sessao.setAttribute("logado", false);
                rd = request.getRequestDispatcher("index.jsp");
            }

        rd.forward(request, response);
    }

   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet LivrariaServelet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet LivrariaServelet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
